<?php
/**
 * Created by coder meng.
 * User: coder meng
 * Date: 2018/4/9 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Db;
use app\blog\model\GoodsClassifyModel;
class GoodsClassifyController extends AdminBaseController
{
    public function index()
    {
        $data=Db::name('GoodsClassify')->select();
        $this->assign("data", $data);
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->request->param("id");
        $data=Db::name('GoodsClassify')->where(array('id'=>$id))->find();
        $this->assign("data", $data);
        return $this->fetch();
    }
    public function addPost()
    {
        if ($this->request->isPost()){
            if (!empty($_POST['classify_name'])){
                //检测类别名称是否已经存在
                $classify_name=$_POST['classify_name'];
                $ischeck=Db::name('GoodsClassify')->where("classify_name='$classify_name'")->count();
                if($ischeck>0){
                    $this->error("该类已存在！");
                    exit;
                }
                $re=Db::name('GoodsClassify')->insert($_POST);
                
                if($re){
                    return $this->success("添加成功！",url('GoodsClassify/index'));
                }else{
                    return $this->success("添加失败！",url('GoodsClassify/index'));
                }
            }else{
                $this->error("请为此类别指定名称和编号！");
            }
        }
    }
    public function editPost()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param("id");

            if (!empty($_POST['classify_name'])) {
                //检测类别名称是否已经存在
                $classify_name=$_POST['classify_name'];
               
                $ischeck=Db::name('GoodsClassify')->where(array('classify_name'=>$classify_name))->count();

                if($ischeck<=0){
                    $re=Db::name('GoodsClassify')->where(array('id'=>$id))->update(['classify_name'=>$classify_name]);
                    $this->success("编辑成功！", url("GoodsClassify/index"));
                }else{
                    $this->error("该类名或编号已存在！");
                    exit;
                }
            }else{
                $this->error("请填写修改内容！");
            }
        }
    }
    public function delete()
    {
        $id = $this->request->param("id");
        $count = Db::name('Goods')->where(['g_category' => $id])->count();
        if ($count > 0) {
            $this->error("该类别下已经有商品！");
        }else{
            $status = Db::name('GoodsClassify')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('GoodsClassify/index'));
            } else {
                $this->error("删除失败！");
            }
        }
    }
    /**
     * 更新类别状态
     * @adminMenu(
     *     'name'   => '更新标签状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '更新状态',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("请选择要修改数据！"));
        }
        $GoodsClassifyModel = new GoodsClassifyModel();
        $GoodsClassifyModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);   
        $this->success(lang("修改成功！"));
    
    }
}